// This script requires one user-made packages: weakiv

cd "./unbundling-replication"

use "data/full_iv.dta", clear

label variable ml "Model Law" 

egen cid = group(iso3c)

xtset cid year

/* Anderson-Rubin Conf. Sets for Table 1 */
// Generates the AR CIs

qui: ivregress 2sls v2x_rule (ml = L.comp_ml_contract) ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
weakiv, gridmin(-1)gridmax(0) gridpoints(500)

qui: ivregress 2sls v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
weakiv, gridmin(-1) gridmax(1) gridpoints(500)

qui: ivregress 2sls v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
				L.trade_ln L.fdi_stock_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
weakiv, gridmin(-1) gridmax(1) gridpoints(500)

qui: ivregress 2sls v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
				L.trade_ln L.fdi_stock_ln ///
				L.growth L.market_ln L.gdppc_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
weakiv, gridmin(-4) gridmax(1) gridpoints(500)

/* Table A8 - LIML Analyses */

qui eststo: ivregress liml v2x_rule (ml = L.comp_ml_contract) ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
estadd local fe 			"\cmark"
estadd local cntrl_legal 	""
estadd local cntrl_econ_dom ""
estadd local cntrl_econ_int ""
estadd scalar r2_adj = round(e(r2_a), .001)

qui estat firststage
mat f = r(singleresults)
estadd scalar fs = round(f[1,4], .01)
mat drop f

weakiv, gridmin(-1) gridmax(0) gridpoints(500) 

qui eststo: ivregress liml v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
estadd local fe 			"\cmark"
estadd local cntrl_legal 	"\cmark"
estadd local cntrl_econ_dom ""
estadd local cntrl_econ_int ""
estadd scalar r2_adj = round(e(r2_a), .001)

qui estat firststage
mat f = r(singleresults)
estadd scalar fs = round(f[1,4], .01)
mat drop f 

weakiv, gridmin(-1) gridmax(0) gridpoints(500) 


qui eststo: ivregress liml v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
				L.trade_ln L.fdi_stock_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
estadd local fe 			"\cmark"
estadd local cntrl_legal 	"\cmark"
estadd local cntrl_econ_dom "\cmark"
estadd local cntrl_econ_int ""
estadd scalar r2_adj = round(e(r2_a), .001)

qui estat firststage
mat f = r(singleresults)
estadd scalar fs = round(f[1,4], .01)
mat drop f 

weakiv, gridmin(-1) gridmax(0) gridpoints(500) 

qui eststo: ivregress liml v2x_rule (ml = L.comp_ml_contract) ///
				L.nyc L.bits_ln ///
				L.trade_ln L.fdi_stock_ln ///
				L.growth L.market_ln L.gdppc_ln ///
			    i.cid i.year ///
				if ml_year > 1996 | ml_year == ., vce(cl cid)
estadd local fe 			"\cmark"
estadd local cntrl_legal 	"\cmark"
estadd local cntrl_econ_dom "\cmark"
estadd local cntrl_econ_int "\cmark"
estadd scalar r2_adj = round(e(r2_a), .001)

qui estat firststage
mat f = r(singleresults)
estadd scalar fs = round(f[1,4], .01)
mat drop f 

 weakiv, gridmin(-4) gridmax(1) gridpoints(500)

esttab est* ///
	using "output/tables/table-A8.tex",  ///
	label s(cntrl_legal cntrl_econ_dom cntrl_econ_int fe N r2_adj fs, ///
	label("Legal" "Economic, Domestic" "Economic, International" "Country and year FE?" "Observations" "Adj. R^2" "F") fmt(%9.0gc)) /// 
	star(* .1 ** .05 *** .01) b(3) se(3) booktabs compress ///
	keep(ml) noomitted  ///
	nomtitles nogap replace
	